﻿@{
    ViewBag.Title = "考勤管理";
    ViewBag.FirstPage = "考勤管理";
    ViewBag.LastPage = "考勤列表";
    Layout = "~/Views/MasterPage.cshtml";
}
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Source/layui/layui.all.js"></script>
<script src="~/Source/layui/layui.js"></script>
<link href="~/Source/layui/css/layui.css" rel="stylesheet" />
<script>
    $(function () {
        $("#btnPunch").click(function () {
            $.post("/Punch/PunchClock", {}, function (returnData) {
                layer.alert(returnData.Message);
                setInterval(function () {
                    window.location.reload();
                }, 1500);
            });
        });
    });
</script>
<div class="am-u-sm-12">
    <button type="button" class="am-btn am-btn-primary am-btn-xs" id="btnPunch">打卡</button>
    <table class="am-table am-table-bd am-table-striped admin-content-table">
        <thead>
            <tr>
                <th>员工</th>
                <th>上班时间</th>
                <th>下班时间</th>
                <th>考勤结果</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>

            @{
                var workList = BLL.PunchManager.QueryListByWork();
                if (workList.Count != 0)
                {
                    foreach (var item in workList)
                    {
                <tr>
                    <td>
                        @{
                        var user = BLL.EmployeeManager.GetUserBasic(item.UserId);
                        var userName = string.Empty;
                        if (user.Id != 0)
                        {
                            userName = user.RealName;
                        }   
                        }
                        @userName
                    </td>
                    <td>@item.StartTime</td>
                    <td>
                        @{
                        if (item.EndTime == null)
                        {
                            @:
                         }
                        else
                        {
                            @:@Convert.ToDateTime(item.EndTime).ToString("yyyy/M/d HH:mm:ss")
                                                                                                                                                                                                                                                                                                                                                                                                                                    }  
                        }

                    </td>
                    <td>
                        @{
                        var punchEntity = BLL.PunchManager.QueryList().Where(p=>p.Id==item.PunchCardId).FirstOrDefault();
                        if (punchEntity != null)
                        {
                            if (item.StartTime != null && item.EndTime != null)
                            {
                                if (Convert.ToDateTime(item.StartTime.ToString("HH:mm:ss")) <= Convert.ToDateTime(punchEntity.time1) && Convert.ToDateTime(Convert.ToDateTime(item.EndTime).ToString("HH:mm:ss")) >= Convert.ToDateTime(punchEntity.time2))
                                {
                            @:<span class="am-badge am-badge-success">考勤正常</span>
                                }
                                else if (Convert.ToDateTime(item.StartTime.ToString("HH:mm:ss")) > Convert.ToDateTime(punchEntity.time1) && Convert.ToDateTime(Convert.ToDateTime(item.EndTime).ToString("HH:mm:ss")) >= Convert.ToDateTime(punchEntity.time2))
                                {
                            @:<span class="am-badge am-badge-danger">迟到</span>
                                }
                                else if (Convert.ToDateTime(item.StartTime.ToString("HH:mm:ss")) <= Convert.ToDateTime(punchEntity.time1) && Convert.ToDateTime(Convert.ToDateTime(item.EndTime).ToString("HH:mm:ss")) < Convert.ToDateTime(punchEntity.time2))
                                {
                            @:<span class="am-badge">早退</span>
                                }
                                else if (Convert.ToDateTime(item.StartTime.ToString("HH:mm:ss")) > Convert.ToDateTime(punchEntity.time1) && Convert.ToDateTime(Convert.ToDateTime(item.EndTime).ToString("HH:mm:ss")) < Convert.ToDateTime(punchEntity.time2))
                                {
                            @:<span class="am-badge">迟到+早退</span>
                                }
                                else if (item.StartTime <= Convert.ToDateTime(punchEntity.time1) && item.EndTime == null)
                                {
                            @:<span class="am-badge">下午旷工</span>
                                }

                            }
                        }   
                            
                        }


                    </td>
                    <td><a data-id="@item.Id" style="cursor:pointer;color:#0094ff">详情</a></td>
                </tr>
                    }
                }
            }
        </tbody>
    </table>
</div>
